package com.awesome.imagedemo.bitmap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.view.View;
import android.widget.ImageView;

import com.awesome.imagedemo.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

/**
 * Author: zhangjingfang
 * Email:zhangjingfang@jeejio.com
 * Date: 2021/12/2
 * Description:BitmapUtil
 */
public class BitmapUtil {
    /**
     * 两个图片的合并
     *
     * @param src  绘制第一张图片
     * @param dst  设置后的画笔，绘制第二张图片
     * @param mode 把画笔设置Xfermode一种模式
     * @return
     */
    public static Bitmap getBimapForXfermode(Bitmap src, Bitmap dst, PorterDuff.Mode mode) {
        int width = src.getWidth();
        int height = src.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        canvas.drawBitmap(src, new Rect(0, 0, src.getWidth(), src.getHeight()), new Rect(0, 0, src.getWidth(), src.getHeight()), paint);
        paint.setXfermode(new PorterDuffXfermode(mode));
        // 第一个Rect裁剪需要绘制图片的部分，第二个Rect绘制到什么位置可拉伸
        canvas.drawBitmap(dst, new Rect(0, 0, dst.getWidth(), dst.getHeight()), new Rect(0, 0, src.getWidth(), src.getHeight()), paint);
        return bitmap;
    }

    /**
     * 两个色块的合并
     *
     * @param src  绘制第一张色块
     * @param dst  设置后的画笔，绘制第二张色块
     * @param mode 把画笔设置Xfermode一种模式
     * @return
     */
    public static Bitmap getColorBlockForXfermode(Bitmap src, Bitmap dst, PorterDuff.Mode mode) {
        int width = src.getWidth();
        int height = src.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        canvas.drawBitmap(src, 0, 0, paint);
        paint.setXfermode(new PorterDuffXfermode(mode));
        // 第一个Rect裁剪需要绘制图片的部分，第二个Rect绘制到什么位置可拉伸
        canvas.drawBitmap(dst, 0, 0, paint);
        return bitmap;
    }


    /**
     * 高斯通过RenderScript实现
     *
     * @param context
     * @param bitmap
     * @return
     */
    public static Bitmap blurFormRenderScript(Context context, Bitmap bitmap) {
        //用需要创建高斯模糊bitmap创建一个空的bitmap
        Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        // 初始化Renderscript，该类提供了RenderScript context，创建其他RS类之前必须先创建这个类，其控制RenderScript的初始化，资源管理及释放
        RenderScript rs = RenderScript.create(context);
        // 创建高斯模糊对象
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        // 创建Allocations，此类是将数据传递给RenderScript内核的主要方 法，并制定一个后备类型存储给定类型
        Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
        Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
        //设定模糊度(注：Radius最大只能设置25.f)
        blurScript.setRadius(15.f);
        // Perform the Renderscript
        blurScript.setInput(allIn);
        blurScript.forEach(allOut);
        // Copy the final bitmap created by the out Allocation to the outBitmap
        allOut.copyTo(outBitmap);
        // recycle the original bitmap
        // bitmap.recycle();
        // After finishing everything, we destroy the Renderscript.
        rs.destroy();
        return outBitmap;
    }

    /**
     * 高斯通过RenderScript实现
     *
     * @param imageView
     * @param bitmap
     * @return
     */
    public static void blurFormGlide(ImageView imageView, Bitmap bitmap) {
        Glide.with(imageView)
                .load(bitmap)
//                .apply(RequestOptions.bitmapTransform(new BlurTransformation(20, 10)))
                .into(imageView);
    }
}
 
 